Skip to contentMethod: remove(NamedResource, Map, URI)
1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.jena;
19:
20: import cz.cvut.kbss.ontodriver.descriptor.AbstractAxiomDescriptor;
21: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
22: import cz.cvut.kbss.ontodriver.jena.connector.SubjectPredicateContext;
23: import cz.cvut.kbss.ontodriver.jena.util.JenaUtils;
24: import cz.cvut.kbss.ontodriver.model.Assertion;
25: import cz.cvut.kbss.ontodriver.model.NamedResource;
26: import cz.cvut.kbss.ontodriver.model.Value;
27: import org.apache.jena.rdf.model.Property;
28: import org.apache.jena.rdf.model.Resource;
29: import org.apache.jena.rdf.model.ResourceFactory;
30:
31: import java.net.URI;
32: import java.util.Collection;
33: import java.util.HashSet;
34: import java.util.Map;
35: import java.util.Set;
36: import java.util.stream.Collectors;
37:
38: /**
39: * This class performs an epistemic removal of statements.
40: * <p/>
41: * Epistemic remove means that only information known to the application is deleted. The assertions in the descriptor
42: * represent this information. Thus, only statements representing these properties are removed from the ontology. Note
43: * that if the descriptor contains an unspecified property assertion, all property assertions related to the subject
44: * individual are removed from the property's context.
45: */
46: class EpistemicAxiomRemover {
47:
48: private final StorageConnector connector;
49:
50: EpistemicAxiomRemover(StorageConnector connector) {
51: this.connector = connector;
52: }
53:
54: /**
55: * Removes statements corresponding to the subject and properties specified by the descriptor.
56: *
57: * @param descriptor Descriptor of statements to remove
58: */
59: void remove(AbstractAxiomDescriptor descriptor) {
60: final Resource subject = ResourceFactory.createResource(descriptor.getSubject().getIdentifier().toString());
61: final Collection<SubjectPredicateContext> toRemove = new HashSet<>();
62: descriptor.getAssertions().forEach(assertion -> {
63: final Property property = ResourceFactory.createProperty(assertion.getIdentifier().toString());
64: toRemove.add(new SubjectPredicateContext(subject, property, descriptor.getAssertionContexts(assertion)
65: .stream().map(URI::toString)
66: .collect(Collectors.toSet())));
67: });
68: connector.removePropertyValues(toRemove);
69: }
70:
71: /**
72: * Removes statements corresponding to the specified values.
73: * <p>
74: * This version removes precisely statements whose subject, property and object match the specified data.
75: *
76: * @param subject Statement subject
77: * @param properties Assertions to values
78: * @param context Context from which to remove the statements
79: */
80: void remove(NamedResource subject, Map<Assertion, Set<Value<?>>> properties, URI context) {
81: final Resource resource = ResourceFactory.createResource(subject.getIdentifier().toString());
82:• if (context != null) {
83: final String strCtx = context.toString();
84: properties.forEach((assertion, values) -> {
85: final Property property = ResourceFactory.createProperty(assertion.getIdentifier().toString());
86: values.forEach(value -> connector
87: .remove(resource, property, JenaUtils.valueToRdfNode(assertion, value), strCtx));
88: });
89: } else {
90: properties.forEach((assertion, values) -> {
91: final Property property = ResourceFactory.createProperty(assertion.getIdentifier().toString());
92: values.forEach(
93: value -> connector
94: .remove(resource, property, JenaUtils.valueToRdfNode(assertion, value), null));
95: });
96: }
97: }
98: }